package com.peanut.baby.mvp.record;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.R;
import com.peanut.baby.model.Photo;
import com.peanut.baby.model.Record;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.PhotoGridAdapter;
import com.peanut.baby.mvp.ask.AskActivity;
import com.peanut.baby.util.TimeUtil;
import com.peanut.baby.widget.UnScrollGridView;
import com.peanut.devlibrary.picker.PhotoPreviewActivity;
import com.peanut.devlibrary.util.StringUtil;
import com.peanut.devlibrary.widget.TitleLayout;
import com.tencent.mars.xlog.Log;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/8/9.
 */

public class RecordDisplayActivity extends BaseMvpActivity implements TitleLayout.OnTitleClickListener,
        AdapterView.OnItemClickListener{
    private static final String TAG = AskActivity.class.getSimpleName();

    @BindView(R.id.title)
    TitleLayout title;


    @BindView(R.id.record_time)
    TextView recordTime;
    @BindView(R.id.record_type_value)
    TextView recordTypeValue;
    @BindView(R.id.record_edit)
    TextView recordEdit;
    private PhotoGridAdapter adapter;
    private List<Photo> photos;


    @BindView(R.id.record_photo_grid)
    UnScrollGridView photoGrid;

    private Record record;

    /**
     * @param ctx
     */
    public static void start(Context ctx, Record record) {
        ctx.startActivity(new Intent(ctx, RecordDisplayActivity.class).putExtra("record", record));
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record_display);
        ButterKnife.bind(this);
        record = (Record) getIntent().getSerializableExtra("record");

        initialViews();
    }

    private void initialViews() {
        title.setTitle("记录");
        title.setOnTitleClickedListener(this);

        recordTime.setText("记录时间: "+ TimeUtil.getDisplayTimeMillis(record.createTime));
        recordTypeValue.setText(record.type);
        recordEdit.setText(record.desc);

        if(!StringUtil.isNullOrEmpty(record.imgUrl)){
            photos = getPhotos();
            adapter = new PhotoGridAdapter(this, photos);
            photoGrid.setAdapter(adapter);
            photoGrid.setOnItemClickListener(this);
        }


    }

    private List<Photo> getPhotos(){
        List<Photo> photos = new ArrayList<>();
        String[] images = record.imgUrl.split(",");
        int len = images.length;
        for(int i=0;i<len;i++){
            Photo photo = new Photo();
            photo.url = images[i];
            photos.add(photo);
        }
        return photos;

    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        PhotoPreviewActivity.start(RecordDisplayActivity.this, getPreviewList(), position);
    }

    /**
     * get list of images for preview
     *
     * @return
     */
    private ArrayList<String> getPreviewList() {
        ArrayList<String> previews = new ArrayList<>();
        for (int i = 0; i < photos.size(); i++) {
            if (!TextUtils.isEmpty(photos.get(i).url)) {
                if (!photos.get(i).url.equals(Photo.FLAG_ADD)) {
                    previews.add(photos.get(i).url);
                }
            } else {
                if (!TextUtils.isEmpty(photos.get(i).path)) {
                    previews.add(photos.get(i).path);
                }
            }
        }
        Log.d(TAG, "getPreviewList photos " + (previews == null ? "null" : " size " + previews.size()));
        return previews;
    }


}